package ejercicio1;

import java.util.ArrayList;
import java.util.List;

public class BibliotecaTriangular extends Biblioteca {

   public BibliotecaTriangular(int tamano) {
      super(2, tamano, tamano*(tamano+1)/2);        //SUMATORIA [1..n] = n*(n+1)/2
   }

   private int getXTriang(int posAbs) {
      int x = 0;
      while (x*(x+1)/2 <= posAbs) {
         x++;
      }
      return x-1;
   }

   @Override
   protected List<Integer> posAbs2PosRel(int posAbs) {
      // TODO Auto-generated method stub
      List<Integer> pr = new ArrayList<Integer>();
      int x = getXTriang(posAbs);
      pr.add(x);
      pr.add(posAbs - x*(x+1)/2);
      return pr;
   }

   @Override
   protected int posRel2PosAbs(List<Integer> posAbs) {
      // TODO Auto-generated method stub
      return posAbs.get(0)*(posAbs.get(0)+1)/2 + posAbs.get(1);
   }

   @Override
   protected boolean validPosRel(List<Integer> posRel) {
      // TODO Auto-generated method stub
      return (posRel.get(0) >= 0) && (posRel.get(0) <= getTamano()-1) &&
             (posRel.get(1) >= 0) && (posRel.get(1) <= posRel.get(0));
   }
}
